/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toolz;

import java.awt.Component;
import java.util.Comparator;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Bunkowski
 */
public class JTableCustomizer {

    JTable jtable;

//    public static void changeComperatorToDoubleIfPossible(JTable table) {
//        TableRowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(table.getModel());
//        table.setRowSorter(rowSorter);
//        for (int i = 0; i < table.getColumnCount(); i++) {
//            try {
//                Double.parseDouble(table.getValueAt(0, i).toString());
//                rowSorter.setComparator(i, new Comparator<String>() {
//                    public int compare(String s1, String s2) {
//                        if ((Double.parseDouble(s1) - Double.parseDouble(s2)) > 0) {
//                            return 1;
//                        } else {
//                            return -1;
//                        }
//                    }
//                });
//            } catch (Exception ex) {
//            }
//
//        }
//    }
    public static void changeComperatorToDoubleIfPossible(JTable table) {

        TableRowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(table.getModel());
        table.setRowSorter(rowSorter);
        try {
            for (int i = 0; i < table.getColumnCount(); i++) {
                boolean allValuesAreDoubles = true;
                for (int j = 0; j < table.getRowCount(); j++) {
                    if (Unknown.isDouble(table.getValueAt(j, i).toString()) == false) {
                        allValuesAreDoubles = false;
                        break;
                    }
                }
                if (allValuesAreDoubles) {
                    rowSorter.setComparator(i, new Comparator<String>() {

                        public int compare(String s1, String s2) {
                            if ((Double.parseDouble(s1) - Double.parseDouble(s2)) > 0) {
                                return 1;
                            } else {
                                return -1;
                            }
                        }
                    });
                }
            }
        } catch (Exception ex) {
        }
    }


   public static int getTotalColumnWidth(JTable table) {
        table.setAutoResizeMode(0);
        int totalSize = 0;
        int size = table.getColumnCount();
        for (int i = 0; i < size; i++) {
            totalSize += getColumnWidth(table, i);
        }
        return totalSize;
    }

   public static int getColumnWidth(JTable table, int colIndex) {
        try {
            TableColumn column = table.getColumnModel().getColumn(colIndex);
            if (column == null) {
                return 0;
            }

            int modelIndex = column.getModelIndex();
            TableCellRenderer renderer, headerRenderer;
            Component component;
            int colContentWidth = 0;
            int headerWidth = 0;
            int rows = table.getRowCount();

//		 Get width of column header
            headerRenderer = column.getHeaderRenderer();
            if (headerRenderer == null) {
                headerRenderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = headerRenderer.getTableCellRendererComponent(
                    table, column.getHeaderValue(), false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width + table.getIntercellSpacing().width;

//		 Get max width of column content
            for (int i = 0; i < rows; i++) {
                renderer = table.getCellRenderer(i, modelIndex);
                Object valueAt = table.getValueAt(i, modelIndex);
                component = renderer.getTableCellRendererComponent(table, valueAt, false, false,
                        i, modelIndex);
                colContentWidth = Math.max(colContentWidth,
                        component.getPreferredSize().width +
                        table.getIntercellSpacing().width);
            }
            int colWidth = Math.max(colContentWidth, headerWidth) + 15;
            //column.setPreferredWidth(colWidth);
            return colWidth;
        //column.setWidth(colWidth);
        //System.out.println("requiredWidth="+colWidth);
        } catch (Exception ex) {
            return 0;
        }
    }

    public static void fitAllColumnWidth(JTable table) {
        table.setAutoResizeMode(0);
        int size = table.getColumnCount();
        for (int i = 0; i < size; i++) {
            fitColumnWidth(table, i);
        }
    }

    public static void fitColumnWidth(JTable table, int colIndex) {
        try {
            TableColumn column = table.getColumnModel().getColumn(colIndex);
            if (column == null) {
                return;
            }

            int modelIndex = column.getModelIndex();
            TableCellRenderer renderer, headerRenderer;
            Component component;
            int colContentWidth = 0;
            int headerWidth = 0;
            int rows = table.getRowCount();

//		 Get width of column header
            headerRenderer = column.getHeaderRenderer();
            if (headerRenderer == null) {
                headerRenderer = table.getTableHeader().getDefaultRenderer();
            }

            Component comp = headerRenderer.getTableCellRendererComponent(
                    table, column.getHeaderValue(), false, false, 0, 0);
            headerWidth = comp.getPreferredSize().width + table.getIntercellSpacing().width;

//		 Get max width of column content
            for (int i = 0; i < rows; i++) {
                renderer = table.getCellRenderer(i, modelIndex);
                Object valueAt = table.getValueAt(i, modelIndex);
                component = renderer.getTableCellRendererComponent(table, valueAt, false, false,
                        i, modelIndex);
                colContentWidth = Math.max(colContentWidth,
                        component.getPreferredSize().width +
                        table.getIntercellSpacing().width);
            }
            int colWidth = Math.max(colContentWidth, headerWidth) + 15;
            column.setPreferredWidth(colWidth);
        //column.setWidth(colWidth);
        //System.out.println("requiredWidth="+colWidth);
        } catch (Exception ex) {
            return;
        }
    }
}
